﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAQ.PROTOCOL;
using DAQ.Common;
using DAQ.Common.BTSR_V1;
using System.Threading;

namespace DAQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        bool CHK = true;
        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.DataReceived += SerialPort1_DataReceived;
            try
            {
                comboBox1.Items.AddRange(SerialPort.GetPortNames());
                comboBox1.SelectedIndex = 0;
            }
            catch (Exception)
            {
            }
            comboBox2.Items.AddRange("4800、9600、19200、38400、43000、56000、56700、57600、115200".Split('、'));
            comboBox2.SelectedIndex = 6;

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if(CHK)
                    {
                        for (int i = 1; i < 5; i++)
                        {
                            serialPort1.SendCmdWithAddr(DAQ.PROTOCOL.ADVANCED_TENSION_READ.GetSendBytes((Byte)i));
                            Thread.Sleep(100);
                        }
                    }
     
                }
            });
        }

        int c = 0;
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            while (serialPort1.BytesToRead > 0)
            {
                this.Invoke(new MethodInvoker(() =>
                {

                    int b = serialPort1.ReadByte();
                    c++;
                    this.textBox1.Text += Convert.ToString(b, 16).PadLeft(2, '0');
                    if (c == 52)
                    {
                        c = 0;
                        this.textBox1.Text += Environment.NewLine;
                    }
                }));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CWMCmn.Inst.IdentifycationProcedure(1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
     //       PC_LINK();
        }

        //private void PC_LINK()
        //{
        //    serialPort1.SendCmdWithAddr(DAQ.PROTOCOL.PC_LINK.GetSendBytes(1));
        //    System.Threading.Thread.Sleep(50);
        //    var sp = serialPort1;
        //    if (sp.BytesToRead > 0)
        //    {
        //        byte[] buffer = new byte[sp.BytesToRead];
        //        int Count = sp.Read(buffer, 0, sp.BytesToRead);
        //        if (Count > 0)
        //        {
        //            PC_LINK ta = new PC_LINK(buffer);

        //            textBox1.Clear();
        //            foreach (var a in ta.AllValues.Keys)
        //            {
        //                this.textBox1.Text += a + ":" + ta.AllValues[a] + Environment.NewLine;
        //            }

        //            string output = string.Join(",", buffer.Select(t => t.ToString("X2")).ToArray());
        //            textBox1.Invoke(new Action(() =>
        //            {
        //                textBox1.Text += output + Environment.NewLine;
        //            }));
        //        }
        //    }
        //}

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                    serialPort1.Close();
                serialPort1.PortName = this.comboBox1.Text;
          //      serialPort1.Open();
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
                //                throw;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                    serialPort1.Close();
                serialPort1.BaudRate = int.Parse(comboBox2.Text);
          //      serialPort1.Open();
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
               ADVANCED_TENSION_READ();
           
        }

        private void ADVANCED_TENSION_READ()
        {
            serialPort1.SendBuffer_9bit(true, 0x01);
            System.Threading.Thread.Sleep(1);
            serialPort1.SendBuffer_9bit(false, 0x3A, 0x3B, 0x00);
            serialPort1.ReadTimeout = 1000;
            System.Threading.Thread.Sleep(200);
            var sp = serialPort1;
            if (sp.BytesToRead > 0)
            {
                byte[] buffer = new byte[sp.BytesToRead];
                int Count = sp.Read(buffer, 0, sp.BytesToRead);
                if (Count > 0)
                {
                    ADVANCED_TENSION_READ ta = new ADVANCED_TENSION_READ(buffer);
                    textBox2.Clear();
                    foreach (var a in ta.AllValues.Keys)
                    {
                        this.textBox2.Text += a + ":" + ta.AllValues[a] + Environment.NewLine;
                    }
                    string output = string.Join(",", buffer.Select(t => t.ToString("X2")).ToArray());
                    textBox2.Invoke(new Action(() =>
                    {
                        textBox2.Text += output + Environment.NewLine;
                    }));
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //    if (serialPort1.IsOpen)
            {
             //   PC_LINK();
                //  ADVANCED_TENSION_READ();
           //     serialPort1.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (CWMCmn.Inst.IdentifycationProcedure(byte.Parse(this.textBox3.Text)) == 0)
            {
                MessageBox.Show("切换更改ID号成功");
            }
            else
            {
                MessageBox.Show("切换失败");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CWMCmn.Inst.CancelIdentifycationProcedure();
        }
       
        private void button6_Click(object sender, EventArgs e)
        {
            //     serialPort1.SendCmdWithAddr(DAQ.PROTOCOL.PC_LINK.GetSendBytes(5));
            this.textBox1.Clear();
            for (int i = 1; i < 5; i++)
            {
                PC_LINK ta = CWMCmn.Inst.GetPC_LINK(i);
                if (ta != null)
                {
                    //textBox1.Clear();
                    textBox1.Text += i + Environment.NewLine;
                    foreach (var a in ta.AllValues.Keys)
                    {
                        this.textBox1.Text += a + ":" + ta.AllValues[a] + Environment.NewLine;
                    }
                }
                System.Threading.Thread.Sleep(50);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.textBox1.Clear();
            for (int i = 1; i < 5; i++)
            {
                var ta = CWMCmn.Inst.GetADVANCE_TENSION_READ(i);
                if (ta != null)
                {
                    //textBox1.Clear();
                    textBox1.Text += i + Environment.NewLine;
                    foreach (var a in ta.AllValues.Keys)
                    {
                        this.textBox1.Text += a + ":" + ta.AllValues[a] + Environment.NewLine;
                    }
                }
                Thread.Sleep(50);
            }
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
           if(this.checkBox1.Checked)
            {
                CHK = true;
            }
           else
            {
                CHK = false;
                Thread.Sleep(100);
                serialPort1.Close();
             
            }
        }
    }


}
